package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserLoginController {
    @Reference
    private IUserInfoCacheService service;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;

    /**
     * 首页
     * @param request   请求
     * @param response  响应
     * @param model     共享
     * @return
     */
    @RequestMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        String token = CookieUtil.getToken(response, request);
        UserInfo user = service.getUserByToken(token);
        // System.out.println(user);
        request.getSession().setAttribute("userInfo", user);
        //tcs
        model.addAttribute("tcs", travelCommendService.getHot5());

        List<Map<String,Object>> travelList = new ArrayList<>();
        List<TravelCommend> travelCommends = travelCommendService.getHot5();
        for (TravelCommend travelCommend : travelCommends) {
            Travel travel = travelService.queryById(travelCommend.getTravelId());
            Map<String,Object> map = new HashMap<>();
            map.put("travel", travel);
            map.put("travelCommend", travelCommend);
            travelList.add(map);
        }
        model.addAttribute("travelList", travelList);

        //sc
        model.addAttribute("sc", strategyCommendService.getHot5().get(0));
        return "index/index";
    }

    /**
     * 前端需要分页  游记
     * @param qo  高级查询
     * @param model 共享
     * @return
     */
    @RequestMapping("/index/travelPage")
    public String travelPage(@ModelAttribute TravelQueryObject qo, Model model) {

        //pageInfo
        model.addAttribute("pageInfo", travelService.query(qo));

        return "/index/travelPageTpl";
    }

    /**
     * es搜索
     * @param model 共享
     * @param qo    高查
     * @return
     */
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            case SearchQueryObject.SEARCH_TYPE_WENDA:
                return searchWebda(model,qo);
            default:
                return searchAll(model,qo);
        }
    }

    //高亮 搜索所有   用户:游记:攻略
    private String searchAll(Model model,SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickName", "destName");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "subTitle");
        Page<StrategyTemplate> strategies = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        //前端传的是一个集合
        SearchResultVO data = new SearchResultVO();
        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategies.getContent());
        data.setTotal(users.getTotalElements()+travels.getTotalElements()+strategies.getTotalElements());
        model.addAttribute("data",data);
        return "/index/searchAll";
    }

    //高亮 搜索用户   昵称:目的地
    private String searchUser(Model model,SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickName", "destName");
        model.addAttribute("page",page);
        return "/index/searchUser";
    }

    //高亮 搜索游记   标题:副标题
    private String searchTravel(Model model,SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "subTitle");
        model.addAttribute("page",page);
        return "/index/searchTravel";
    }


    //高亮 搜索攻略   标题:副标题:简介
    private String searchStrategy(Model model,SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "/index/searchStrategy";
    }

    //高亮 搜索问答   标题:副标题:tag
    private String searchWebda(Model model,SearchQueryObject qo) {
        Page<QuestionTemplate> page = searchService.searchWithHighlight(QuestionTemplate.INDEX_NAME, QuestionTemplate.TYPE_NAME, QuestionTemplate.class, qo, "title", "tag", "summary");
        model.addAttribute("page", page);
        return "/index/searchQuestion";
    }


    //搜索目的地
    private String searchDest(Model model,SearchQueryObject qo) {
        SearchResultVO data = new SearchResultVO();
        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        long total= 0;
        if (dest!=null){
            List<StrategyTemplate> template =strategySearchService.findByName(dest.getName());
            total += template.size();
            if (template.size()>5){
                template.subList(0,5);
            }
            List<TravelTemplate> travelTemplates = travelTemplateSearchService.findByDestName(dest.getName());
            total += travelTemplates.size();
            if (travelTemplates.size()>5){
                travelTemplates.subList(0,5);
            }
            List<UserInfoTemplate> userInfoTemplates = userInfoSearchService.findByName(dest.getName());
            total += userInfoTemplates.size();
            if (userInfoTemplates.size()>5){
                userInfoTemplates.subList(0,5);
            }
            data.setStrategys(template);
            data.setTravels(travelTemplates);
            data.setUsers(userInfoTemplates);
            data.setTotal(total);
            model.addAttribute("data",data);
        }
        model.addAttribute("dest",dest);
        return "/index/searchDest";
    }


}
